package com.gitee.feizns.quickstart.ding.talk.api.v10.yida;

import com.aliyun.dingtalkyida_1_0.models.GetInstanceByIdRequest;
import com.aliyun.dingtalkyida_1_0.models.GetInstanceByIdResponseBody;
import com.aliyun.dingtalkyida_1_0.models.StartInstanceRequest;
import com.gitee.feizns.quickstart.ding.talk.api.v10.oauth2.AccessToken;
import com.gitee.feizns.quickstart.ding.talk.api.v10.yida.processes.instances.InstancesInfos;
import com.gitee.feizns.quickstart.ding.talk.api.v10.yida.processes.instances.Start;

/**
 * 流程 API
 * @author feizns
 * @since 2024/12/5
 */
public class ProcessesApi {

    /**
     * 表单
     */
    private final YiDaConfig yiDaConfig;

    /**
     * token
     */
    private final AccessToken accessToken;

    /**
     * 表单API
     * @param yiDaConfig Yi da config
     */
    public ProcessesApi(YiDaConfig yiDaConfig) {
        this(yiDaConfig, new AccessToken(yiDaConfig.getConfig()));
    }

    /**
     * 表单API
     * @param yiDaConfig Yi da config
     * @param accessToken 访问令牌
     */
    public ProcessesApi(YiDaConfig yiDaConfig, AccessToken accessToken) {
        this.yiDaConfig = yiDaConfig;
        this.accessToken = accessToken;
    }

    /**
     * 开始流程
     * @param req 要求
     * @return {@link String}
     */
    public String start(StartInstanceRequest req) {
        req.setAppType(yiDaConfig.getAppType());
        req.setLanguage(yiDaConfig.getLanguage());
        req.setSystemToken(yiDaConfig.getSystemToken());
        if ( req.getUserId() == null )
            req.setUserId(yiDaConfig.getUserId());
        return Start.execute(accessToken.get(), req);
    }

    /**
     * 获取由编号
     * @param id 编号
     * @return {@link GetInstanceByIdResponseBody}
     */
    public GetInstanceByIdResponseBody getById(String id) {
        return getById(id, new GetInstanceByIdRequest());
    }

    /**
     * 获取由编号
     * @param id 编号
     * @param request 请求
     * @return {@link Object}
     */
    public GetInstanceByIdResponseBody getById(String id, GetInstanceByIdRequest request) {
        request.setAppType(yiDaConfig.getAppType());
        request.setSystemToken(yiDaConfig.getSystemToken());
        request.setUserId(yiDaConfig.getUserId());
        request.setLanguage(yiDaConfig.getLanguage());
        return InstancesInfos.execute(accessToken.get(), id, request);
    }

}
